package com.douqu.game.core.config.goods;

import com.douqu.game.core.config.card.CardConfig;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2017-10-17 10:37
 */
public class PropConfig extends Goods {
    /**
     * 道具类型
     * 1为消耗品、2为任务道具、4为卡片碎片、5士兵经验道具、6饰品经验道具
     */
    public int type;

    /***
     * 当前已存放数量
     */
    public int count;

    public int effectValue;

    public String price;

    /**
     * 分解返回的英魂数量（当为装备的时候才会有）
     */
    public GoodsData[] soulData;

    /**
     * 药水使用的配置（当type为药水类型的时候才会有）
     */
    public GoodsData[] liquidData;

    /**
     * 时效道具使用的配置（当type为时效道具类型的时候才会有）
     */
    public GoodsData timePropData;
    /**
     * 主要阵营宝箱在用
     */
    public int[] campDropGroup;
    /**
     * 复活信息
     */
    public int[] reviveData;

    public int[] rechargeData;
    /**
     * 碎片对应的卡牌ID
     */
    private CardConfig cardConfig;

    @Override
    public boolean check() {
        if (type == ConstantFactory.PROP_TYPE_BAG) {
            if (DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.DROP_GROUP_KEY, effectValue) == null) {
                LogUtils.error("PropConfig check effectValue is null -> id:" + id + " name:" + name);
                return false;
            }
        }


        if (type == ConstantFactory.PROP_TYPE_LIQUID) {
            if (liquidData == null) {
                LogUtils.error("PropConfig check effectValue is null -> id:" + id + " name:" + name);
                return false;
            }
            for (GoodsData data : liquidData) {
                if (DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.ATTRIBUTE_KEY, data.type) == null) {
                    LogUtils.error("PropConfig check liquidData is null -> id:" + data.type + " value:" + data.id);
                    return false;
                }
            }
        }

        if (type == ConstantFactory.PROP_TYPE_TIME_PROP) {
            if (timePropData == null) {
                LogUtils.error("PropConfig check timePropData is null -> id:" + id + " name:" + name);
                return false;
            }
        }
        return true;
    }

    @Override
    public String getVariable(String key) {
        if ("soulData".equals(key) || "liquidData".equals(key)) {
            return "";
        } else
            return super.getVariable(key);
    }

    @Override
    public boolean setVariable(String key, String value) {
        if ("effectValue".equals(key)) {
            soulData = new GoodsData[0];
            if (type == ConstantFactory.PROP_TYPE_EQUIP_SYN_EXP) {
                soulData = LoadUtils.loadGoodDataToArray(key, value);
                return LoadUtils.checkGoods(soulData, this.getClass(), key, id);
            } else if (type == ConstantFactory.PROP_TYPE_LIQUID) {
                liquidData = LoadUtils.loadGoodDataToArray(key, value);
                return true;
            } else if (type == ConstantFactory.PROP_TYPE_BAG) {
                effectValue = Integer.parseInt(value);
            } else if (type == ConstantFactory.PROP_TYPE_TIME_PROP) {
                timePropData = LoadUtils.loadGoodDataToArray(key, value)[0];
            } else if (type == ConstantFactory.PROP_TYPE_CAMP_BOX) {
                //如果是阵营宝箱 三个掉落组id
                campDropGroup = LoadUtils.loadToArray(key, value);
            } else if (type == ConstantFactory.PROP_TYPE_REVIVE) {
                // 复活类道具
                reviveData = LoadUtils.loadToArray(key, value);
            } else {
                effectValue = Integer.parseInt(value);
            }
            return true;
        } else if ("price".equals(key)) {
            if(value != null && !value.equals("0")){
                rechargeData = LoadUtils.loadToArray(key, value);
                return true;
            }
        }
        return super.setVariable(key, value);
    }

    public CardConfig getCardConfig() {
        return cardConfig;
    }

    public void setCardConfig(CardConfig cardConfig) {
        this.cardConfig = cardConfig;
    }

    @Override
    public String toString() {
        return "Prop{" +
                "type=" + type +
                ", count=" + count +
                "} " + super.toString();
    }
}
